﻿package net.wangit.view.element;

import net.wangit.Row;
import net.wangit.framework.config.BeanFactory;
import net.wangit.util.Checker;
import net.wangit.view.unit.UnitComponent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class ElementFactory {

	private static Log log = LogFactory
			.getLog(net.wangit.view.element.ElementFactory.class);

	public ElementFactory() {
	}

	public static ElementComponent getElement(Row row, UnitComponent uc) {
		String id = row.get("eid");
		String componentId = row.get("c");
		ElementComponent component = null;
		try {
			component = BeanFactory.getElementComponent(componentId);
		} catch (Exception e) {
			log.error((new StringBuilder("could not load element component: "))
					.append(componentId).append(" for ").append(uc.getDomain())
					.append("/").append(uc.getId()).append("/").append(id)
					.toString(), e);
			throw new ElementConfigurationException((new StringBuilder(
					"could not load element component: ")).append(componentId)
					.toString(), uc.getDomain(), uc.getId(), id,
					"component id", e);
		}
		try {
			component.setId(id);
			component.setIsToolbarElement(row.getInt("b", 1) == 0);
			component.setName(row.get("n"));
			component.setHasAccessPrivilege("1".equals(row.get("au")));
			component.setRejectResult(row.getInt("aum", 9));
			component.setIsAction("1".equals(row.get("a")));
			component.setTitle(row.get("t"));
			component.setCustom(row.get("cu"));
			component.setCss(row.get("css"));
			component.setWidth(row.getInt("w", -1));
			component.setHeight(row.getInt("h", -1));
			component.setLink(row.get("l"));
			component.setDefaultValue(row.get("d", ""));
			component.setRequired("1".equals(row.get("vr")));
			component.setMarkRequired(!"0".equals(row.get("vm")));
			component.setMinLength(row.getInt("v1", -1));
			component.setMaxLength(row.getInt("v2", -1));
			component.setScale(row.getInt("v3", -1));
			component.setValidator(row.get("vv"));
			component.setCustomValidator(row.get("vc"));
			component.setValidationExpression(row.get("ve"));
			component.setAddLabel(!"0".equals(row.get("al")));
			component.setScanShowMode(row.getInt("ms", 1));
			component.setEditShowMode(row.getInt("me", 3));
			component.setAddShowMode(row.getInt("ma", 3));
			component.setGroupId(row.get("g"));
			component.setSort(row.getInt("ls", 0));
			component.setTdAlign(row.getInt("wa", 0));
			component.setTdVerticalAlign(row.getInt("wv", 0));
			component.setTdColSpan(row.getInt("wcs", 1));
			component.setTdRowSpan(row.getInt("wrs", 1));
			component.setTdHeight(row.getInt("wh", -1));
			component.setTdHeightMode(row.getInt("whm", -1));
			component.setTdWidth(row.getInt("ww", -1));
			component.setTdWidthMode(row.getInt("wwm", -1));
			component.setTdPaddingTop(row.getInt("wpt", -1));
			component.setTdPaddingLeft(row.getInt("wpl", -1));
			component.setTdPaddingBottom(row.getInt("wpb", -1));
			component.setTdPaddingRight(row.getInt("wpr", -1));
			String temp = row.get("wcu");
			if (!Checker.isEmpty(temp))
				component.setTdCustom(temp);
			temp = row.get("wc");
			if (!Checker.isEmpty(temp))
				component.setTdCss(temp);
			temp = row.get("wp");
			if (!Checker.isEmpty(temp))
				component.setPrefix(temp);
			temp = row.get("ws");
			if (!Checker.isEmpty(temp))
				component.setSuffix(temp);
			component.configure(row);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("some error found while create a element");
		}
		return component;
	}

}
